package com.xmtlxx.tms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.tms.domain.bo.LinePoiBo;
import com.xmtlxx.tms.domain.bo.LinesBo;
import com.xmtlxx.tms.domain.vo.CarrierVo;
import com.xmtlxx.tms.domain.vo.LinesVo;
import com.xmtlxx.tms.service.ICarrierService;
import com.xmtlxx.tms.service.ILinePoiService;
import com.xmtlxx.tms.service.ILinesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 线路Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "线路控制器", tags = {"线路管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/tms/lines")
public class LinesController extends BaseController {

    private final ILinesService iLinesService;

    private final ILinePoiService iLinePoiService;

    private final ICarrierService iCarrierService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<LinesVo> initBean(){
        LinesVo vo = new LinesVo();
        if(LoginHelper.getLoginUser().getRolePermission().contains("smscys")){
            vo.setIsShow(false);
            CarrierVo carrierVo = iCarrierService.selectCarrierByDeptId(LoginHelper.getDeptId());
            vo.setCarrierId(carrierVo.getId());
        }else {
            vo.setIsShow(true);
        }
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询线路列表
     */
    @ApiOperation("查询线路列表")
    @SaCheckPermission("tms:lines:list")
    @GetMapping("/list")
    public TableDataInfo<LinesVo> list(@Validated(QueryGroup.class) LinesBo bo, PageQuery pageQuery) {
        return iLinesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出线路列表
     */
    @ApiOperation("导出线路列表")
    @SaCheckPermission("tms:lines:export")
    @Log(title = "线路", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated LinesBo bo, HttpServletResponse response) {
        List<LinesVo> list = iLinesService.queryList(bo);
        ExcelUtil.exportExcel(list, "线路", LinesVo.class, response);
    }

    /**
     * 获取线路详细信息
     */
    @ApiOperation("获取线路详细信息")
    @SaCheckPermission("tms:lines:query")
    @GetMapping("/{id}")
    public R<LinesVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        LinesVo vo = iLinesService.queryById(id);
        LinePoiBo bo = new LinePoiBo();
        bo.setLineId(id);
        bo.setDelFlag("0");
        vo.setLinePois(iLinePoiService.queryList(bo));
        if(LoginHelper.getLoginUser().getRolePermission().contains("smscys")){
            vo.setIsShow(false);
        }else {
            vo.setIsShow(true);
        }
        return R.ok(vo);
    }

    /**
     * 新增线路
     */
    @ApiOperation("新增线路")
    @SaCheckPermission("tms:lines:add")
    @Log(title = "线路", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LinesVo vo) {
        LinesBo bo = new LinesBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iLinesService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改线路
     */
    @ApiOperation("修改线路")
    @SaCheckPermission("tms:lines:edit")
    @Log(title = "线路", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LinesVo vo) {
        LinesBo bo = new LinesBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iLinesService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除线路
     */
    @ApiOperation("删除线路")
    @SaCheckPermission("tms:lines:remove")
    @Log(title = "线路", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iLinesService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
